import di from "./di";

export default class diaoluo extends Laya.Script {

    constructor() { 
        super(); 
        this.PrefabArr=[]
        this.GameOver=false;
        // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
    }
    onAwake(){
        Laya.SoundManager.playMusic("res/Sounds/bgm.mp3",0);
        this.loadPrefab();
    }
    loadPrefab(){
        var pathArr=[
            "prefab/di.json",
            "prefab/jing.json",
            "prefab/di1.json",
            "prefab/di2.json",
            "prefab/di3.json",
        ];
        var infoArr=[]
        for(var i=0;i<pathArr.length;i++){
            infoArr.push({url:pathArr[i],type:Laya.Loader.PREFAB})
        }
        Laya.loader.load(infoArr,Laya.Handler.create(this,function(resule){
            for(var i=0;i<pathArr.length;i++){
                this.PrefabArr.push(Laya.loader.getRes(pathArr[i]))
            }
                    
            this.tanTime=this.getRandom(800,1000);
        Laya.timer.loop(this.tanTime,this,function(){
            this.spawn();
             this.tanTime=this.getRandom(300,800);
        })
        }))
    }

    spawn(){
        var arrX=[300,600,900,1200,1500,1800];
        this.y=100;
        this.x=arrX[this.getRandom(0,arrX.length-1)];

        var Index=this.getRandom(0,this.PrefabArr.length-1);
        var Di=Laya.Pool.getItemByCreateFun(Index.toString(),function(){return this.PrefabArr[Index].create()},this);
        Laya.stage.addChild(Di);
        Di.pos(this.x,this.y);
        Di.getComponent(di).Init(Index.toString());
    }

    getRandom(min,max){
        var value=Math.random()*(max-min);
        value=Math.round(value);
        return min+value;
        }

}